package com.angel.activity;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import com.angel.adapter.MyAdapter;
import com.angel.entity.RemindBean;
import com.angel.entity.SearchActivityList;
import com.angel.entity.Task;
import com.angel.port.IActivity;
import com.angel.util.BaseUtil;
import com.angel.util.UserInfoUtil;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

/** 我的活动 */
public class MyActivity extends Activity implements IActivity{

	/** 设置按钮 */
	private Button setting_btn,my_join_btn,my_interest_btn;
	private ListView listView = null;
	/** 自定义Adapter */
	private MyAdapter myAdapter = null;
	private Context context;
	private SearchActivityList searchActivityList;
	private HashMap<String,String> map;
	private ProgressDialog progressDialog;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_activity);
		context=MyActivity.this;
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		progressDialog=new ProgressDialog(context);
		progressDialog.setMessage("加载数据中,请稍后...");
		
		
		listView = (ListView) findViewById(R.id.my_ListView);
		//启动服务，请求数据,默认是请求参加活动请求
		request("1");
		
	    my_join_btn = (Button) findViewById(R.id.my_join);
	    my_interest_btn = (Button) findViewById(R.id.my_interest);
		setting_btn = (Button) findViewById(R.id.setting_btn);
		setting_btn.setOnClickListener(new ButtonActionListener());
		listView.setOnItemClickListener(new ListItemOnClickListener());
		my_join_btn.setSelected(true);
	}
	public class ListItemOnClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			 
			String activityId=String.valueOf(searchActivityList.getResult().get(arg2).getActivityId());
			Intent intent = new Intent();
			intent.putExtra("activityId", activityId);//传递活动Id
			intent.setClass(MyActivity.this, ActivityDetail.class);
			startActivity(intent);
			
		}
		
	}
	/** 按钮事件 */
	public class ButtonActionListener implements OnClickListener {
		public void onClick(View v) {
			if (v.getId() == R.id.setting_btn) {
				Intent intent = new Intent();
				intent.setClass(MyActivity.this, SetUpActivity.class);
				startActivity(intent);
				String remindTime=UserInfoUtil.getValue(context, "remindTime", "0");
				if(remindTime.equals("0")){//如果初始为0,则从服务器端获取
					map=new HashMap<String,String>(2);
					map.put("userId", UserInfoUtil.getValue(context, "userId", "0"));
					map.put("remindTime", "0");
					BaseUtil.startService(Task.MY_ACTIVITY, MyActivity.this, map);//启动请求
				}
				
			}
		}
	}
	@Override
	public void init() {
		
	}
	@Override
	public void refresh(Object... params) {
		 String isSucess=BaseUtil.getValueFromJSON((String)params[0]);
		
		 if(isSucess.equals("2")){//如果网络状态出错
			 BaseUtil.makeToast(MyActivity.this, "对不起,网络连接超时,请稍后重试...");
			 progressDialog.dismiss();
			 return ;
		 }if(isSucess.equals("4")){//如果为活动时间提醒请求
			 Type type=new TypeToken<RemindBean>(){}.getType();
			 RemindBean rb=(RemindBean) BaseUtil.getEntityFromJson((String)params[0], type);
			 map=new HashMap<String,String>(1);
			 map.put("remindTime", String.valueOf( rb.getRemindTime()));
			 UserInfoUtil.setValue(context,map);
			 
		 }else{//如果为获取参加或感兴趣活动
			 Type type=new TypeToken<SearchActivityList>(){}.getType();
				searchActivityList=(SearchActivityList)BaseUtil.getEntityFromJson((String)params[0], type);
			    if(searchActivityList.getIsSuccess().equals("1")){
			    	myAdapter = new MyAdapter(context,searchActivityList.getResult());
					listView.setAdapter(myAdapter);
			    }else{
			    	BaseUtil.makeToast(MyActivity.this, "对不起,网络加载出错,请稍后再试");
			    }
		 }
		  progressDialog.dismiss();
	}
	/*点击参加活动按钮，请求数据*/
	public void takePart(View v){
		request("1");
		my_join_btn.setSelected(true);
		my_interest_btn.setSelected(false);
	}
	/*点击感兴趣*/
	public void takeInterest(View v){
		request("0");
		my_join_btn.setSelected(false);
		my_interest_btn.setSelected(true);
	}
	/*根据请求是否为interest，将参数置0或1*/
	public void request(String choose){
		map=new HashMap<String,String>(3);
		map.put("userId", UserInfoUtil.getValue(context, "userId", "0"));
		map.put("joined", choose);
		progressDialog.show();//显示进度条
		BaseUtil.startService(Task.MY_ACTIVITY, this, map);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			BaseUtil.showExitDialog(this);
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		super.onPause();
		 MobclickAgent.onPause(this);
	}
}
